package cc.linker.steplink.crm.merchant.controller.integral;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchant.service.integral.IntegralGroupService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/integral/group")
public class IntegralGroupController {

    @Autowired
    private IntegralGroupService integralGroupService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 积分商城分区列表
     *
     */
    @GetMapping("/getList")
    public List<Map> getList(HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return integralGroupService.getList(orgId);
    }

    /**
     * 保存积分商城分区
     *
     */
    @PostMapping("/save")
    public void save(HttpServletRequest request, @RequestBody Map<String, Object> param){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        integralGroupService.save(param);
    }

    /**
     * 移除积分商城分区
     *
     */
    @PostMapping("/delete")
    public void delete(@RequestBody Map<String, Object> param){
        integralGroupService.delete(param);
    }

    /**
     *修改积分商城分区状态
     *
     */
    @PostMapping("/updateStatus")
    public void updateStatus(@RequestBody Map<String, Object> param){
        integralGroupService.updateStatus(param);
    }

    /**
     * 积分商城分区排序
     *
     * @param ： id 专区id
     * @param ： sort 排序
     * @param ： type 0 向下 1 向上
     *
     */
    @PostMapping("/updateSort")
    public void updateSort(@RequestBody Map<String, Object> param, HttpServletRequest request)
    {
        if(StringUtils.isEmpty(param.get("id")) || StringUtils.isEmpty(param.get("sort")) || StringUtils.isEmpty(param.get("type"))){
            throw GlobalException.build("参数不能为空！");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Long id = Long.valueOf(param.get("id").toString());
        Integer sort = Integer.valueOf(param.get("sort").toString());
        Integer type = Integer.valueOf(param.get("type").toString());
        String orgId = userinfo.getOrgId();
        integralGroupService.updateSort(id,sort,type,orgId);
    }


    /**
     * 积分商城分区商品列表
     *groupId 专区id
     */
    @GetMapping("/getGoodsList")
    public Object getGoodsList(@RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                               @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,Long groupId){
        return integralGroupService.getGoodsList(groupId,pageIndex,pageSize);
    }

    /**
     * 添加积分商城分区商品
     *groupId 专区id
     *integralGoodsIds 积分商品列表
     */
    @PostMapping("/addIntegralGoods")
    public void addIntegralGoods(@RequestBody Map<String, Object> param, HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        integralGroupService.addIntegralGoods(param);
    }

    /**
     * 移除积分商城分区商品
     *groupId 专区id
     *integralGoodsId 积分商品
     */
    @PostMapping("/deleteIntegralGoods")
    public void deleteIntegralGoods(@RequestBody Map<String, Object> param){
        integralGroupService.deleteIntegralGoods(param);
    }



    /**
     * 积分商城分区商品排序
     *
     * @param ： groupId 专区id
     * @param ： integralGoodsId 积分商品id
     * @param ： sort 排序
     * @param ： type 0 向下 1 向上
     *
     */
    @PostMapping("/updateGoodsSort")
    public void updateGoodsSort(@RequestBody Map<String, Object> param, HttpServletRequest request)
    {
        if(StringUtils.isEmpty(param.get("groupId")) || StringUtils.isEmpty(param.get("integralGoodsId")) || StringUtils.isEmpty(param.get("sort")) || StringUtils.isEmpty(param.get("type"))){
            throw GlobalException.build("参数不能为空！");
        }
        Long groupId = Long.valueOf(param.get("groupId").toString());
        Long integralGoodsId = Long.valueOf(param.get("integralGoodsId").toString());
        Integer sort = Integer.valueOf(param.get("sort").toString());
        Integer type = Integer.valueOf(param.get("type").toString());
        integralGroupService.updateGoodsSort(groupId,integralGoodsId,sort,type);
    }




}
